//*************************************************************************
// File Name: ReportingService.cpp
// This is the implementation file for the member functions
// of Class ReportingService
// "Group 3"
// CS 1C
// March 25, 2010
// Group Project: SuperWarehouse
// Due: March 25, 2010
// - KZ's comment
//*************************************************************************

#include "ReportingService.h"
#include "Member.h"
#include <dirent.h>

//  private class members
//	filenameForSalesReportDay(time_t timestamp)

ReportingService::ReportingService() {

}

ReportingService::~ReportingService() {
}

string ReportingService::listAvailableSalesReports() {
	const int BUF_LEN = 1000;
	DIR *dp;
	struct dirent *dirp;
	char buf[BUF_LEN];
	const char *prefix = "sales_";
	string ret, tmp;

	ret = string();

	if ((dp = opendir(REPORTS_DIR)) == NULL) {
		sprintf(buf, "Unable to read directory %s", REPORTS_DIR);
		errorMsg(buf);
		return ret;
	}

	while ((dirp = readdir(dp)) != NULL) {
		if (strlen(dirp->d_name) <= strlen(prefix))
			continue;
		strncpy(buf, dirp->d_name, strlen(prefix));
		buf[strlen(prefix) + 1] = 0;
		if (strcmp(buf, prefix))
			continue;
		tmp = string(dirp->d_name);
		chomp(tmp);
		ret.append(tmp).append("\n");
	}

	return ret;
}

void ReportingService::printSalesReport(string reportName) {
	char path[1000];
	string line;
	ifstream inFile;

	sprintf(path, "%s/%s", REPORTS_DIR, reportName.c_str());
	inFile.open(path, ios::in);
	if (!inFile) {
		errorMsg("Unable to open report file");
		return;
	}
	while (!inFile.eof()) {
		getline(inFile, line, '\n');
		chomp(line);
		cout << line << endl;
	}
	inFile.close();
}

bool ReportingService::writeSalesReportForDay(time_t timestamp,
		SaleItemList *salesList, int numSuper, int numBasic) {

	//ofstream outFile;
	int i,n ;
	GenericList<SaleItemList::SaleItem> *items;
	SaleItemList::SaleItem *currItem;
	string fileName;
	int totalQty;
	float tax, gross, totalTax, totalSales, totalGross;
	char buf[1000];

	totalQty = 0;
	totalSales = 0.0;
	totalTax = 0.0;
	totalGross = 0.0;

	fileName = salesReportFilenameForDay(timestamp);
	ofstream outFile(fileName.c_str(), ios::trunc);

	items = salesList->getItems();
	n = items->getNumElements();

	outFile << "Sales List for " << dateStringFromTimestamp(timestamp) << endl;
	outFile << setfill('=') << setw(79) << right << ' ' << setfill(' ')
			<< endl;
	outFile << left << setw(40) << "Item Name" << setw(5) << "Qty"
			<< setw(10) << "Sales" << setw(10) << "Tax" << setw(10)
			<< "Total" << endl;
	outFile << right << setfill('=') << setw(79) << ' ' << setfill(' ') << endl;
	outFile << left;

	for (i = 0; i < n; i++) {
		currItem = items->retrieveElementAtIndex(i);

		totalSales += currItem->totalSales;
		totalQty += currItem->totalQty;
		tax = currItem->totalSales * (SALES_TAX_PERCENT / 100);
		gross = currItem->totalSales + tax;
		totalTax += tax;
		totalGross += gross;

		outFile << setw(40) << currItem->name << setw(5) << currItem->totalQty;
		sprintf(buf, "$%0.2f", currItem->totalSales);
		outFile << setw(10) << buf;
		sprintf(buf, "$%0.2f", tax);
		outFile << setw(10) << buf;
		sprintf(buf, "$%0.2f", gross);
		outFile << setw(10) << buf << endl;

	}

	outFile << right << setfill('-') << setw(79) << ' ' << setfill(' ')
			<< endl;
	outFile << left << setw(40) << "TOTAL" << setw(5) << totalQty;
	sprintf(buf, "$%0.2f", totalSales);
	outFile << setw(10) << buf;
	sprintf(buf, "$%0.2f", totalTax);
	outFile << setw(10) << buf;
	sprintf(buf, "$%0.2f", totalGross);
	outFile << setw(10) << buf << endl;
	outFile << right << setfill('-') << setw(79) << ' ' << setfill(' ')
			<< endl;
	outFile << "Shoppers with Basic membership: " << numBasic << endl;
	outFile << "Shoppers with Super membership: " << numSuper << endl;

	outFile << right << setfill('=') << setw(79) << ' ' << setfill(' ')
			<< endl;

	outFile.close();

	return true;
}

void ReportingService::printMemberList(MemberList *members) {
	int i, n;
	Member *currMember;

	n = members->getNumMembers();

	cout << "MEMBER LIST" << endl;
	cout << "Printing details for " << n << " members." << endl << endl;

	for (i = 0; i < n; i++) {
		currMember = members->retrieveMemberAtIndex(i);
		cout << currMember->toString() << endl;
	}

	cout << endl;
}

void ReportingService::printExpiringMemberList(MemberList *members) {
	int i, n;
	Member *currMember;
	float totalDues;
	char buf[200];

	n = members->getNumMembers();
	totalDues = 0.0;

	cout << "EXPIRING MEMBER LIST" << endl;
	cout << "Printing details for " << n << " members." << endl << endl;

	for (i = 0; i < n; i++) {
		currMember = members->retrieveMemberAtIndex(i);
		totalDues += currMember->getDues();
		cout << currMember->toString() << endl;
	}

	cout << endl;

	sprintf(buf, "$%0.2f", totalDues);
	cout << "Total Renewal Dues Expected: " << buf << endl << endl;

}
